package com.powernode.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.powernode.domain.PickAddr;
import com.powernode.mapper.PickAddrMapper;
import com.powernode.service.PickAddrService;
import com.powernode.utils.AuthUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * 内容:
 * 作者: cy
 */
@Service
@Slf4j
public class PickAddrServiceImpl extends ServiceImpl<PickAddrMapper, PickAddr> implements PickAddrService {

    @Autowired
    private PickAddrMapper pickAddrMapper;

    /**
     * 分页查询自提点列表
     * @param page
     * @param pickAddr
     * @return
     */
    @Override
    public Page<PickAddr> selectPickAddrPage(Page<PickAddr> page, PickAddr pickAddr) {
        return pickAddrMapper.selectPage(page, new LambdaQueryWrapper<PickAddr>()
                .eq(pickAddr.getShopId() != 1, PickAddr::getShopId, pickAddr.getShopId())
                .like(StringUtils.hasText(pickAddr.getAddrName()), PickAddr::getAddrName, pickAddr.getAddrName())
        );
    }

    /**
     * 新增自提点
     * @param pickAddr
     */
    @Override
    public void addPickAddr(PickAddr pickAddr) {
        Long userId = AuthUtil.getUserIdOrShopId(true);
        log.info("新增自提点{}，用户id为{}，店铺id为{}", JSON.toJSONString(pickAddr), userId, pickAddr.getShopId());
        pickAddrMapper.insert(pickAddr);
    }
}
